﻿var Order = new Object();
Order = function () {

    this.NameObj = null
    this.SurnameObj = null

    this.TownObj = null;
    this.StreetObj = null;
    this.ZipObj = null;
    this.EmailObj = null;
    this.PhoneObj = null;

    this.OrderBtnObj = null;
    this.CaptchaObj = null;

    this.Pdf_invoiceIdObj = null;
    this.ByelawIdObj = null;

    this.ConfirmOrderUrl = JsConst.ConfirmOrderUrl;

    this.Init = function (NameId, SurnameId, TownId, StreetId, ZipId, EmailId, PhoneId, CaptchaId, Pdf_invoiceId, ByelawId) {

        this.NameObj = $("#" + NameId);
        this.SurnameObj = $("#" + SurnameId);

        this.TownObj = $("#" + TownId);
        this.StreetObj = $("#" + StreetId);
        this.ZipObj = $("#" + ZipId);
        this.EmailObj = $("#" + EmailId);
        this.PhoneObj = $("#" + PhoneId);

        this.CaptchaObj = $("#" + CaptchaId);

        this.Pdf_invoiceIdObj = $("#" + Pdf_invoiceId);
        this.ByelawIdObj = $("#" + ByelawId);

    }


    this.GetInputModel = function () {
        return {
            Name: this.NameObj.val(),
            Surname: this.SurnameObj.val(),
            Town: this.TownObj.val(),
            Street: this.StreetObj.val(),
            Zip: this.ZipObj.val(),
            Email: this.EmailObj.val(),
            Phone: this.PhoneObj.val(),
            Captcha: this.CaptchaObj.val(),
            Pdf: this.Pdf_invoiceIdObj.val(),
            Byelaw: this.ByelawIdObj.val()
        }
    }

    this.Validate = function () {

    if (!this.ValidateName()) { 
        return false
    }

    if (!this.ValidateSurname()) {
        return false
    }

    if (!this.ValidateTown()) {
        return false
    }

    if (!this.ValidateStreet()) {
        return false
    }

    if (!this.ValidateZip()) {
        return false
    }

    if (!this.ValidateEmail()) {
        return false
    }

    if (!this.ValidateCaptcha()) {
        return false
    }


    
    
        return true;
    }


    /*--************* Validation ************************-- */

    this.ValidateCaptcha = function () {

        this.SetWhite(this.CaptchaObj);

        if (this.CaptchaObj.val() != JsConst.Captcha) {
            this.SetRed(this.CaptchaObj);
            return false;
        }

        return true;
    }

    this.ValidateName = function () {

        this.SetWhite(this.NameObj);

        if (this.NameObj.val() == null || this.NameObj.val() == '') {
            this.SetRed(this.NameObj);
            return false;
        }

        return true;
    }

    this.ValidateSurname = function () {

        this.SetWhite(this.SurnameObj);

        if (this.SurnameObj.val() == null || this.SurnameObj.val() == '') {
            this.SetRed(this.SurnameObj);
            return false;
        }

        return true;
    }

    this.ValidateTown = function () {

        this.SetWhite(this.TownObj);

        if (this.TownObj.val() == null || this.TownObj.val() == '') {
            this.SetRed(this.TownObj);
            return false;
        }

        return true;
    }

    this.ValidateStreet = function () {

        this.SetWhite(this.StreetObj);

        if (this.TownObj.val() == null || this.StreetObj.val() == '') {
            this.SetRed(this.StreetObj);
            return false;
        }

        return true;
    }

    this.ValidateZip = function () {

        this.SetWhite(this.ZipObj);

        if (this.ZipObj.val() == null || this.ZipObj.val() == '') {
            this.SetRed(this.ZipObj);
            return false;
        }

        return true;
    }

    this.ValidateEmail = function () {

        this.SetWhite(this.EmailObj);

        if (this.EmailObj.val() == null || this.EmailObj.val() == '') {
            this.SetRed(this.EmailObj);
            return false;
        }

        return true;
    }

    this.SetRed = function (inputObj) {
        inputObj.css("background", "#FAEBD7");
    }
    this.SetWhite = function (inputObj) {
        inputObj.css("background", "white");
    }






    /*--************* ADD ARTICLE TO CART ************************-- */

    this.ConfirmOrder = function (button) {

        this.OrderBtnObj = $(button);

        if (!this.Validate()) {
            return;
        }


        this.SendJSON(this.ConfirmOrderUrl, this.CallBack_OrderConfirm);

        this.OrderBtnObj.css("diplay", "none");
    }

    this.CallBack_OrderConfirm = function (callBackData, self) {

    }

    /*--************* SEND JSON ************************-- */
    this.SendJSON = function (url, callBack) {
        if (url + '' == '') {
            alert(JsConst.ErrorMsg_PriceList_SendJSON_UrlEmpty);
            return;
        }
        /* Map the js object to serverside class representation */
        var address = this.GetInputModel();

        if (address == null) {
            alert(JsConst.ErrorMsg_PriceList_SendJSON_ArticleObjectNull);
            return;
        }

        var json = JSON.stringify(address);
        var self = this;

        $.ajax({
            url: url,
            cache: false,
            type: 'POST',
            dataType: 'json',
            data: json,
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                /* get the result and do some magic with it*/
                callBack(data, self);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                callBack(xhr.status + ":" + thrownError, self);
            }
        });
    }

}